package com.example.secondhandshop.ui.fragment;




import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.secondhandshop.R;
import com.example.secondhandshop.bean.Item1;
import com.example.secondhandshop.model.BoughtRequest;
import com.example.secondhandshop.ui.adapter.BoughtGoodsAdapter;


import java.util.ArrayList;
import java.util.List;

public class BoughtFragment extends Fragment {
    private RecyclerView recyclerView;
    private BoughtGoodsAdapter adapter;
    private List<Item1> itemList;
    private BoughtRequest itemModel;
    private int currentPage = 1; // 当前页数
    private boolean isLoading = false; // 是否正在加载
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_bought, container, false);
        recyclerView = view.findViewById(R.id.recyclerViewSoldItems1);
        itemList = new ArrayList<>();
        adapter = new BoughtGoodsAdapter(itemList);


        // 使用 GridLayoutManager 设置为1列
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 1);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        itemModel = new BoughtRequest();
        loadProducts(currentPage, 3, null, null, view); // 加载商品，指定当前页和页面大小

        // 上拉加载更多
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!recyclerView.canScrollVertically(1) && !isLoading) {
                    currentPage++; // 页数加1
                    loadProducts(currentPage, 3, null, null, view); // 加载更多商品
                }
            }
        });

//        // 判断数据是否为空，显示空视图
//        checkEmptyView(view);

        return view;
    }

    private void loadProducts(int current, int size, String keyword, String typeId, View view) {
        int userId = 624; // 假设用户 ID，可以根据实际情况获取
        isLoading = true; // 设置为正在加载

        itemModel.getItems(userId, current, size, keyword, typeId, new BoughtRequest.OnProductsFetchedListener() {

            @Override
            public void onSuccess(List<Item1> items) {
                Log.e("BoughtFragment", "onSuccess: " + items);
                // 确保在主线程中更新 UI
                if (getActivity() != null&&items!= null) {
                    getActivity().runOnUiThread(() -> {
                        if (current == 1) {
                            itemList.clear();// 清空列表以重新加载
                        }
                        itemList.addAll(items); // 添加新商品
                        adapter.notifyDataSetChanged(); // 刷新适配器
                        isLoading = false; // 设置为不再加载
                        // 判断数据是否为空，显示空视图
                        checkEmptyView(view);

                    });
                }
            }

            @Override
            public void onFailure(String error) {
                // 确保在主线程中更新 UI
                if (getActivity() != null) {
                    getActivity().runOnUiThread(() -> {

                        Toast.makeText(getContext(), "加载失败: " + error, Toast.LENGTH_SHORT).show();
                        isLoading = false; // 设置为不再加载

                        // 判断数据是否为空，显示空视图
                        checkEmptyView(view);

                    });
                }
            }
        });
    }

    public void checkEmptyView(View view) {
        TextView emptyView = view.findViewById(R.id.emptyView1);
        if (itemList.isEmpty()) {
            emptyView.setVisibility(View.VISIBLE);
        } else {
            emptyView.setVisibility(View.GONE);
        }
    }
}
